package com.example.opengl.avengine;

import android.os.Build;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TimeUtils {
    private static Map<String, RecordTag> mTimeRecordMap = new HashMap<>();
    private static StringBuilder result = new StringBuilder();
    private static final long MAX_FRAME_COUNT = 100;

    private static class RecordTag {
        public String tag;
        public long recordStart;
        public long recordEnd;
        public long lastDuration;
        public long recordTotal;
        public long num;
        public long recordMax;
        public long recordHistoryMax;
    }

    public static HashMap<String, Object> BuildMap(Object... configs) {
        if (configs == null) return null;
        HashMap<String, Object> maps = new HashMap<>();
        for (int i = 0; i < configs.length / 2; i++) {
            maps.put((String) configs[2 * i], configs[2 * i + 1]);
        }
        return maps;
    }




    //log日志拼接
    public static String LogStr(String... filter) {
        result.delete(0, result.length());
        Iterator<String> iter = mTimeRecordMap.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            boolean needPrint = filter.length == 0;
            for (String f : filter) {
                needPrint = needPrint || key.contains(f);
            }
            if (needPrint) {
                result.append(key + ":" + mTimeRecordMap.get(key).lastDuration +
                        " average:" + Math.floor(mTimeRecordMap.get(key).recordTotal * 1.f / mTimeRecordMap.get(key).num)
                        + "#max:" + Math.floor(mTimeRecordMap.get(key).recordMax)
//                        + "#h_max:" + Math.floor(mTimeRecordMap.get(key).recordHistoryMax)
                        + "\n\n");
            }
        }
        return result.toString();
    }

    public void Clear() {
        mTimeRecordMap.clear();
    }

    public static long leftWithoutTime(long time) {
        if (time % 1000000 == 0) {
            return time;
        }
        return leftTime(time);
    }

    public static long rightWithoutTime(long time) {
        if (time % 1000000 == 0) {
            return time;
        }
        return rightTime(time);
    }

    public static long leftTime(long time) {
        return Math.max(0, time - 1) / 1000000 * 1000000;
    }

    public static long rightTime(long time) {
        return (time + 1000000) / 1000000 * 1000000;
    }

    public static long quzheng(long time) {
        return time / 1000000 * 1000000;
    }

    public static long engineTime2FileTime(long pos, AVComponent avComponent) {
        return pos - avComponent.getEngineStartTime() + avComponent.getClipStartTime();
    }

}
